package org.ocelot.tunes4j.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

class NioBenchmark1 {
  static int size = 60; // MB
  static int tests = 5;

  interface Copier {
    public void copy(File s, File t) throws IOException;
  }

  static class NioCopier implements Copier {
    public void copy(File s, File t) throws IOException {
    	long start = System.currentTimeMillis();
      FileChannel in = (new FileInputStream(s)).getChannel();
      FileChannel out = (new FileOutputStream(t)).getChannel();
      in.transferTo(0, s.length(), out);
      in.close();
      out.close();
		long end = System.currentTimeMillis();
		long elapsedTime = end - start;
		System.out.println("Copy process using: 'NioCopier', Finished In: " + elapsedTime/ 1000f + " seconds:");
		
    }
  }
  

  static class IoCopier implements Copier {
    final int BUFF_SIZE = 5 * 1024 * 1024; // 5MB
    final byte[] buffer = new byte[BUFF_SIZE];

    public void copy(File s, File t) throws IOException {
     	long start = System.currentTimeMillis();
        
    	InputStream in = new FileInputStream(s);
      FileOutputStream out = new FileOutputStream(t);

      while (true) {
        int count = in.read(buffer);
        if (count == -1)
          break;
        out.write(buffer, 0, count);
      }
      out.close();
      in.close();
      	
      	long end = System.currentTimeMillis();
		long elapsedTime = end - start;
		System.out.println("Copy process using: 'IO', Finished In: " + elapsedTime/ 1000f + " seconds:");
    }
    
  }

  public static void main(String[] arg) throws IOException {
    Copier io = new IoCopier();
    Copier nio = new NioCopier();
    nio.copy(new File("C:\\Documents and Settings\\Hugo\\Mis documentos\\vlc-1.0.1-win32.exe"), new File("C:\\Documents and Settings\\Hugo\\Mis documentos\\t.bak"));
    io.copy(new File("C:\\Documents and Settings\\Hugo\\Mis documentos\\vlc-1.0.1-win32.exe"), new File("C:\\Documents and Settings\\Hugo\\Mis documentos\\t1.bak"));
  }


}